package com.xry.match.service;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xry.common.bean.Stadium;
import com.xry.common.service.StadiumService;
import com.xry.match.mapper.StadiumMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86152
* @description 针对表【stadium】的数据库操作Service实现
* @createDate 2025-09-04 19:58:40
*/
@Service
@DubboService
public class StadiumServiceImpl extends ServiceImpl<StadiumMapper, Stadium>
        implements StadiumService {
    @Override
    public Stadium getStadiumById(Integer id) {
        LambdaQueryWrapper<Stadium> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Stadium::getId, id).eq(Stadium::getDeleted, 0);
        return super.getOne(queryWrapper);
    }


    @Override
    public boolean updateStadiumById(Stadium stadium) {
        return super.updateById(stadium);
    }

    @Override
    public boolean deleteStadiumById(Integer id) {
        Stadium stadium = super.getById(id);
        if (stadium != null){
            stadium.setDeleted(1);
            return super.updateById( stadium);
        }
        return false;
    }

    @Override
    public boolean addStadium(Stadium stadium) {
        return super.save(stadium);
    }

    @Override
    public List<Stadium> getAll() {
        return super.list();
    }

    @Override
    public IPage<Stadium> getPage(Stadium stadium, IPage<Stadium> page) {
        LambdaQueryWrapper<Stadium> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (stadium != null) {
            if (StringUtils.hasText(stadium.getName())) {
                queryWrapper.like(Stadium::getName, stadium.getName());
            }
            if (StringUtils.hasText(stadium.getAdress())) {
                queryWrapper.like(Stadium::getAdress, stadium.getAdress());
            }
            if (StringUtils.hasText(stadium.getMessage())) {
                queryWrapper.like(Stadium::getMessage, stadium.getMessage());
            }
        }

        // 只查询未删除的记录
        queryWrapper.eq(Stadium::getDeleted, 0);

        // 按创建时间倒序排列
        queryWrapper.orderByDesc(Stadium::getCreatetime);

        return super.page(page, queryWrapper);
    }
}


